import { Fragment, useState } from "react";
import { Form, DatePicker, Input, Button, Radio } from "antd";
import './index.css'
import { useNavigate, useLocation } from "react-router-dom";
import dayjs from "dayjs";
import axios from "axios";
import { addDayReportApi } from "@/api/api";

function AddDayReport() {
    const [count, setcount] = useState([])
    const [value, setValue] = useState(1);
    const initState = useLocation().state
    // console.log(initState.date);
    let doneStr = "请输入本日完成的内容"
    let riskStr = "描述可能有风险的问题，需要帮助的内容，以及延迟的部分和预估延迟的情况"
    const navigate = useNavigate()
    //保存时间
    const onChange = (date) => {
        if (date) {
            setcount(date)
        } else {
            console.log('Clear');
        }
    };
    const layout = {
        labelCol: { span: 6 },
        wrapperCol: { span: 14 }
    };
    const onfinish = (fieldsvalue) => {
        //发送添加请求
        addDayReportApi({
        reportTime:dayjs(fieldsvalue.date).format('YYYY-MM-DD'),
        content:fieldsvalue.done,
        isBalance:fieldsvalue.delay,
        riskInfo:fieldsvalue.todo}).then((res)=>{
            console.log(res);
        })
        console.log(fieldsvalue);
        return navigate('/home/weekly/daily_paper')
    }
    //返回按钮
    const goback = () => {
        navigate(-1)
    }
    return (
        <Fragment>
            <Form layout="horizontal" {...layout} disabled={initState? true:false} colon={false} onFinish={onfinish} initialValues={initState ? { todo: initState.todo, date:dayjs.unix(initState.date), done: initState.done, delay: initState.delay == "否" ? '0' : '1' } : {}}>
                <Form.Item label=" "><h1 style={{opacity:initState? 0:1}}>新增日报</h1></Form.Item>
                <Form.Item label="日报时间：" required={true} name="date" rules={[{ required: true, message: "时间不能为空!" }]}>
                    <DatePicker
                        placeholder={"日报时间"}
                        onChange={onChange}
                        allowClear={true} />
                </Form.Item>
                <Form.Item label="完成内容描述：" required={true} name="done" rules={[{ required: true, message: "完成内容不能为空!" }]}>
                    <Input.TextArea className="txt" rows={5} cols={33} placeholder={doneStr} />
                </Form.Item>
                <Form.Item label="与计划相比是否有偏差" required={true} name="delay" rules={[{ required: true, message: "是否有偏差不能为空!" }]}>
                    <Radio.Group value={value} >
                        <Radio value={'1'}>是</Radio>
                        <Radio value={'0'}>否</Radio>

                    </Radio.Group>
                </Form.Item>
                <Form.Item label="风险内容描述：" required={true} name="todo" rules={[{ required: true, message: "风险内容不能为空!" }]}>
                    <Input.TextArea className="txt" rows={5} cols={33} placeholder={riskStr} />
                </Form.Item>
                <Form.Item label=" ">
                    <Button type="primary" htmlType="submit">确认提交</Button>
                    <Button disabled={false} onClick={goback} style={{marginLeft:"5rem"}}>返回</Button>
                </Form.Item>
            </Form>
        </Fragment>
    )
}

export default AddDayReport